package com.victor.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.victor.dao.BybookcouponDao;
import com.victor.entity.Bybookcoupon;
import com.victor.entity.Mybybookcoupon;
import com.victor.service.BybookcouponService;
import com.victor.utils.DateUtils;

@Service
public class BybookcouponServiceImpl implements BybookcouponService {

	@Autowired
	private BybookcouponDao bybookcouponDao;

	// 查询购书卷
	@Override
	public List<Bybookcoupon> getBybookcouponList() {
		return bybookcouponDao.getBybookcouponList();
	}

	// 添加购书卷
	@Override
	public int addBybookcoupon(Bybookcoupon bybookcoupon) {
		return bybookcouponDao.addBybookcoupon(bybookcoupon);
	}

	// 批量删除
	@Override
	public boolean batchDelete(int[] ids) {
		return bybookcouponDao.batchDelete(ids) > 0 ? true : false;
	}

	// 获取我的购书卷
	@Override
	public List<Mybybookcoupon> getmybybookcoupon(String userid) {
		return bybookcouponDao.getmybybookcoupon(userid);
	}

	// 更新我的购书卷表中购书卷状态
	@Override
	public void updateBookcoupon() {
		// 获取购书卷所有信息(可使用)
		List<Mybybookcoupon> mybybookcoupons = bybookcouponDao.getAllmybybookcoupon();
		for (Mybybookcoupon mybybookcoupon : mybybookcoupons) {
			if (mybybookcoupon.getBybookcoupon() == null) {
				continue;
			}
			// 获取有效期结束时间
			String valid_end = mybybookcoupon.getBybookcoupon().getValid_end();
			if (valid_end == null) {
				continue;
			}
			// 如果当前日期在有效期结束之后，则购书卷失效
			Date valid_end_date = DateUtils.StrToDate(valid_end + " 23:59:59");// 有效期那天最后一秒

			if (valid_end_date.before(new Date())) {// valid_end_date早于datenow返回true
				// 如果条件成立，则购书卷失效
				bybookcouponDao.updateShixiao(mybybookcoupon.getId());
			}
		}
	}

	@Override
	public Mybybookcoupon findById(String couponid) {
		return bybookcouponDao.findById(couponid);
	}

	@Override
	public boolean updateCouponFlag(String couponid, String flag) {
		return bybookcouponDao.updateCouponFlag(couponid, flag) > 0 ? true : false;
	}

	@Override
	public Bybookcoupon selBybookcoupon() {
		return bybookcouponDao.selBybookcoupon();
	}

	@Override
	public List<Bybookcoupon> selBybookcouponAll() {
		return bybookcouponDao.selBybookcouponAll();
	}

	@Override
	public boolean updBybookcouponById(int id, int number,int newAlready_number) {
		return bybookcouponDao.updBybookcouponById(id, number,newAlready_number);
	}
}
